import { _decorator, Node, Sprite, TiledMap, UIOpacity, UITransform, Vec2, Vec3 } from 'cc';
import { PanelModel } from '../../Basic/UI/PanelModel';
import { LevelItem } from './LevelItem';
import { PoolFactory } from '../../Basic/Component/PoolFactory';
import { PoolElementType } from '../Config/GameEnum';
import { DataMgr } from '../../Basic/Manager/DataManager';
import { GameParams } from '../../Basic/Config/DataConfig';
import { Tool } from '../../Util/Tool';
const { ccclass } = _decorator;

@ccclass('LevelModel')
export class LevelModel extends PanelModel {
  className: string = 'LevelModel';
  loadRes: string = 'maingame/level/LevelPanel';
  // levelMapTrans: UITransform;
  // levelMap: TiledMap;
  // poolFac: PoolFactory;
  // lineParent: Node;
  // pointParent: Node;
  // btnStart: Node;
  // levelLines: Node[];
  // levelItems: LevelItem[];
  // levelPos: Vec3[];
  loadTime: number;

  // readMapPoint() {
  //   while (this.levelPos && this.levelPos.length) this.levelPos.pop();

  //   this.levelPos = [];

  //   const matchTxt = this.levelMap._tmxFile.tmxXmlStr.match(/(x=\S+ y=\S[^>]+)/g);
  //   matchTxt.forEach((txt) => {
  //     const ret = txt.match(/[^xy=" ]+/g);
  //     const pos = ret.map(Number);
  //     this.levelPos.push(new Vec3(pos[0] - this.levelMapTrans.contentSize.width / 2, -pos[1] + this.levelMapTrans.contentSize.height / 2));
  //   });
  // }

  // initLevelItems() {
  //   while (this.levelItems && this.levelItems.length) this.poolFac.push(this.levelItems.pop().node);
  //   while (this.levelLines && this.levelLines.length) this.poolFac.push(this.levelLines.pop());
  //   this.levelItems = [];
  //   this.levelLines = [];

  //   const gameLevel = DataMgr.gameParams.get(GameParams.gameLevel);
  //   this.levelPos.forEach((pos, i) => {
  //     const item = this.poolFac.pop(PoolElementType.LEVEL_ITEM, 'maingame/level', 'LevelItem');
  //     const script = item.getComponent(LevelItem);
  //     script.setLevelData(gameLevel - 1 > i, i + 1);
  //     script.itemCar.active = gameLevel >= this.levelPos ? i === this.levelPos.length - 1 : gameLevel - 1 === i;
  //     const scale = i % 5 === 0 ? 1 : 0.5;
  //     script.itemSpr.node.setScale(scale, scale);
  //     this.pointParent.addChild(item);
  //     this.levelItems.push(script);
  //     item.setPosition(pos);

  //     if (i != 0) {
  //       const line = this.poolFac.pop('LevelLine', 'maingame/level', 'LevelLine');
  //       const lastPos = this.levelPos[i - 1];
  //       const sub = Tool.Vec2Sub(new Vec2(lastPos.x, lastPos.y), new Vec2(pos.x, pos.y));
  //       line.angle = (Math.atan2(sub.y, sub.x) * 180) / Math.PI - 90;
  //       script.itemSpr.grayscale = i > gameLevel - 1;
  //       line.getComponent(Sprite).grayscale = i > gameLevel - 1;
  //       line.getComponent(UIOpacity).opacity = i > gameLevel - 1 ? 128 : 255;
  //       this.lineParent.addChild(line);
  //       line.setPosition(pos);
  //       line.getComponent(UITransform).height = sub.length();
  //       // line.getComponent(UITransform).width = 7.5;
  //       this.levelLines.push(line);
  //     }
  //   });
  // }

  // selectLevelItem(selectItem: LevelItem) {
  //   // let showBtn: boolean = false;
  //   this.levelItems.forEach((item) => {
  //     if (item === selectItem) {
  //       item.selectLevel();
  //       // showBtn = !item.isPass;
  //     } else item.rendererUI();
  //   });
  //   // this.btnStart.active = showBtn;
  // }
}
